#ifndef OPERATION
#define OPERATION
#include <iostream>
#include <map>
#include "../../base/TemplateParser/Parser.h"
using namespace std;
class Operation:public Parser
{
	public:
		string parseOperation(string);
		bool isOperation(string);
	private:
		string parseoperation(string);

 };
bool Operation::isOperation(string in){
	string::iterator it=in.begin();
	string op="+-*/=";
	int openbracket=0;
	while(it!=in.end()){
		if(*it=='(')openbracket++;
		else if(*it==')')openbracket--;
		else if(StringProcessor::isInString(*it,op) && openbracket==0)return true;
		it++;
	}
	return false;
}

string Operation::parseOperation(string in){
	string left;
	string right=in;
	//check equal '='
	string::size_type pos=string::npos;
	pos=in.find_first_of('=');
	if(pos!=string::npos){
		left=in.substr(0,pos);
		pos++;
		right=in.substr(pos,in.size()-pos);
	}


	//parse operation
	right=parseoperation(right);
	//setview for left
	if(left!="")setViewData(left,right);


	//return
    return right;
}

string Operation::parseoperation(string in){

    	BO::parseitem op;
		//remove ()
    	in=removeCurlyBracket(in);
    	//cout<<in<<endl;
		string::size_type pos=0;
		string::size_type firstpos=string::npos;
		int open_bracket_count=0;
		string::iterator it = in.begin();
		while(it!=in.end()){
			//check if is in bracket
			if(*it=='(')open_bracket_count++;
			else if(*it==')')open_bracket_count--;
			//if | is not belong to any bracket then get pos
			else if(*it=='+' && open_bracket_count==0 && pos!=0){firstpos=pos;op.direction="+";}
			else if(*it=='-' && open_bracket_count==0 && pos!=0){firstpos=pos;op.direction="-";}
			else if(*it=='*' && open_bracket_count==0 && pos!=0 && (op.direction!="+" && op.direction!="-")){firstpos=pos;op.direction="*";}
			else if(*it=='/' && open_bracket_count==0 && pos!=0 && (op.direction!="+" && op.direction!="-")){firstpos=pos;op.direction="/";}
			it++;
			pos++;
		}

		if(firstpos!=string::npos){
			op.out=parseoperation(in.substr(0,firstpos));
			firstpos++;
			op.in=parseoperation(in.substr(firstpos,in.size()-firstpos));
			//if(in=="test<\"f\"&&\"e\"==test2")cout<<compare.in<<"aaa"<<endl;
		}
		//if no found then check is string
		else if(isstring(in)){
			//cout<<"size:"<<in.size()-1<<in.at(0)<<'-'<<in.at(1)<<'-'<<in.at(2)<<endl;
			return removequote(in);
		}
		//check function
		else if(isFunction(in)){
			//cout<<compare.direction<<compare.out<<endl;
			return parseFunction(in);
		}
		//if no function, no direction
		//get from dataview
		else{
			//check is number:
			//cout<<in<<"-operation"<<endl;
			if(StringProcessor::isnum(in))return in;
			else return getViewData(in);
		}
		bool isleftnum=false;
		bool isrightnum=false;
		//check is number
		if(isstring(op.out))op.out=removequote(op.out);
		else isleftnum=StringProcessor::isnum(op.out);
		if(isstring(op.in))op.in=removequote(op.in);
		else isrightnum=StringProcessor::isnum(op.in);
		//operation:
		//cout<<op.out<<'-'<<op.in<<'-'<<op.out+op.in<<endl;
		if(op.direction=="+"){
			if(isleftnum && isrightnum)
				return StringProcessor::itoa(StringProcessor::atoi(op.out)+StringProcessor::atoi(op.in));
			else
				return op.out+op.in;
		}
		if(op.direction=="-"){
			if(isleftnum && isrightnum)
				return StringProcessor::itoa(StringProcessor::atoi(op.out)-StringProcessor::atoi(op.in));
			else{
				//throw exception
				throw "cannot use Operation \"-\" for string value";
			}
		}
		if(op.direction=="*"){
			if(isleftnum && isrightnum)
				return StringProcessor::itoa(StringProcessor::atoi(op.out)*StringProcessor::atoi(op.in));
			else{
				//throw exception
				throw "cannot use Operation \"*\" for string value";
			}
		}
		if(op.direction=="/"){
			if(isleftnum && isrightnum)
				return StringProcessor::itoa(StringProcessor::atoi(op.out)/StringProcessor::atoi(op.in));
			else{
				//throw exception
				throw "cannot use Operation \"/\" for string value";
			}
		}
    	return "";
    }

/*
//condition parser















*/


#endif
